package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.LableAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.LableLibBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LableActivity extends BaseActivity {
    private String token;
    private RecyclerView rc_lable;
    private LableAdapter adapter;
    private ImageView icon_back;
    private ArrayList<String> selectIds = new ArrayList<>();
    private ArrayList<String> selectNmae = new ArrayList<>();
    private Button bt_finish;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        bt_finish.setOnClickListener(view -> {
            //点击完成按钮
            Intent intent = new Intent(LableActivity.this, UserInfoActivity.class);
            intent.putExtra("selectName", selectNmae);
            intent.putExtra("selectid", selectIds);
            setResult(RESULT_OK,intent);
            finish();
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_lable);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        getRequest();
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        rc_lable = findViewById(R.id.rc_lable);
        icon_back = findViewById(R.id.icon_back);
        bt_finish = findViewById(R.id.bt_finish);
        getlablelib(token, EXTRA, 0);
    }

    private void getlablelib(String token, String extra, int host) {
        NetUtils2.getInstance().getApiService().getLableLib(token, extra, host)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr41", decryptStr);
                        Gson gson = new Gson();
                        LableLibBean bean = gson.fromJson(decryptStr, LableLibBean.class);
                        GridLayoutManager gridLayoutManager = new GridLayoutManager(LableActivity.this, 4, GridLayoutManager.VERTICAL, false);
                        rc_lable.setLayoutManager(gridLayoutManager);
                        adapter = new LableAdapter(bean.getData().getLableLib());
                        rc_lable.setAdapter(adapter);

                        adapter.setOnAddClickListener(new LableAdapter.OnAddClickListener() {
                            @Override
                            public void onItemClicks(int position, int id, boolean isselect, String name) {
                                if (selectIds.size() == 3 && isselect) {
                                    Toast.makeText(LableActivity.this, R.string.mi_zuiduozhinengxuanzhongjige, Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                bean.getData().getLableLib().get(position).setSelect(isselect);
                                adapter.setData(bean.getData().getLableLib());
                                adapter.notifyDataSetChanged();

                                if (isselect) {
                                    //选中的标签ID
                                    selectIds.add(id + "");
                                    selectNmae.add(name);
                                } else {
                                    //取消选中
                                    if (selectIds != null && selectIds.size() > 0) {
                                        for (int i = 0; i < selectIds.size(); i++) {
                                            if (selectIds.get(i).equals(id + "")) {
                                                selectIds.remove(i);
                                                selectNmae.remove(i);
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
